home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Newswatcher 2.0b22 / NW Source / Source / dragutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-02  |  5.4 KB  |  216 lines  |  [TEXT/MMCC]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     dragutil.c
  4.  
  5.     This reusable module contains miscellaneous Drag Manager utility routines.
  6.     
  7.     Copyright © 1994, Northwestern University.
  8.  
  9. ----------------------------------------------------------------------------*/
  10.  
  11. #include <Folders.h>
  12. #include <Drag.h>
  13.  
  14. #include "def.h"
  15. #include "dragutil.h"
  16. #include "memutil.h"
  17. #include "teutil.h"
  18. #include "drawutil.h"
  19.  
  20.  
  21.  
  22. /*----------------------------------------------------------------------------
  23.     GetDropLocationDirectory
  24.     
  25.     Given an 'alis' drop location AEDesc, return the volume reference
  26.     number and directory ID of the directory.
  27.     
  28.     Entry:    dropLocation = pointer to 'alis' AEDesc record.
  29.     
  30.     Exit:    function result = error code.
  31.             *volumeID = volume reference number.
  32.             *directoryID = directory ID.
  33.             
  34.     From Apple "HFS Drag Sample" sample code.
  35. ----------------------------------------------------------------------------*/
  36.  
  37. OSErr GetDropLocationDirectory (AEDesc *dropLocation, short *volumeID, 
  38.     long *directoryID)
  39. {
  40.     OSErr err = noErr;
  41.     AEDesc targetDescriptor;
  42.     FSSpec targetLocation;
  43.     CInfoPBRec getTargetInfo;
  44.     
  45.     err = AECoerceDesc(dropLocation, typeFSS, &targetDescriptor);
  46.     if (err != noErr) return err;
  47.     
  48.     BlockMoveData(*targetDescriptor.dataHandle, &targetLocation, sizeof(FSSpec));
  49.     
  50.     err = AEDisposeDesc(&targetDescriptor);
  51.     if (err != noErr) return err;
  52.     
  53.     getTargetInfo.dirInfo.ioNamePtr = targetLocation.name;
  54.     getTargetInfo.dirInfo.ioVRefNum = targetLocation.vRefNum;
  55.     getTargetInfo.dirInfo.ioFDirIndex = 0;
  56.     getTargetInfo.dirInfo.ioDrDirID = targetLocation.parID;
  57.     
  58.     err = PBGetCatInfoSync(&getTargetInfo);
  59.     if (err != noErr) return err;
  60.     
  61.     *directoryID = getTargetInfo.dirInfo.ioDrDirID;
  62.     *volumeID = targetLocation.vRefNum;
  63.     return noErr;
  64. }
  65.  
  66.  
  67.  
  68. /*----------------------------------------------------------------------------
  69.     DragTargetWasTrash
  70.     
  71.     Check to see if the target of a drag and drop was the Finder trashcan.
  72.     
  73.     Entry:    dragRef = drag reference.
  74.     
  75.     Exit:    function result = true if target was trash.
  76. ----------------------------------------------------------------------------*/
  77.  
  78. Boolean DragTargetWasTrash (DragReference dragRef)
  79. {
  80.     AEDesc dropLocation;
  81.     OSErr err = noErr;
  82.     long dropDirID, trashDirID;
  83.     short dropVRefNum, trashVRefNum;
  84.     
  85.     err = GetDropLocation(dragRef, &dropLocation);
  86.     if (err != noErr) return false;
  87.     
  88.     if (dropLocation.descriptorType == typeNull) goto exit;
  89.     
  90.     err = GetDropLocationDirectory(&dropLocation, &dropVRefNum, &dropDirID);
  91.     if (err != noErr) goto exit;
  92.     
  93.     err = FindFolder(dropVRefNum, kTrashFolderType, false, &trashVRefNum,
  94.         &trashDirID);
  95.     if (err != noErr) goto exit;
  96.     
  97.     if (dropVRefNum != trashVRefNum || dropDirID != trashDirID) goto exit;
  98.  
  99.     AEDisposeDesc(&dropLocation);
  100.     return true;
  101.  
  102. exit:
  103.  
  104.     AEDisposeDesc(&dropLocation);
  105.     return false;
  106. }
  107.  
  108.  
  109.  
  110. /*----------------------------------------------------------------------------
  111.     MyGetFlavorDataHandle
  112.     
  113.     Get flavor data.
  114.     
  115.     Entry:    dragRef = drag reference.
  116.             itemRef = item reference.
  117.             theType = flavor type.
  118.     
  119.     Exit:    function result = error code.
  120.             *flavorData = handle to flavor data.
  121. ----------------------------------------------------------------------------*/
  122.  
  123. OSErr MyGetFlavorDataHandle (DragReference dragRef, ItemReference itemRef,
  124.     FlavorType theType, Handle *flavorData)
  125. {
  126.     OSErr err = noErr;
  127.     Handle h = nil;
  128.     Size len;
  129.     
  130.     err = GetFlavorDataSize(dragRef, itemRef, theType, &len);
  131.     if (err != noErr) goto exit;
  132.     
  133.     err = MyNewHandle(len, &h);
  134.     if (err != noErr) goto exit;
  135.     
  136.     MyHLock(h);
  137.     err = GetFlavorData(dragRef, itemRef, theType, *h, &len, 0);
  138.     if (err != noErr) goto exit;
  139.     MyHUnlock(h);
  140.     
  141.     *flavorData = h;
  142.     return noErr;
  143.     
  144. exit:
  145.  
  146.     MyDisposeHandle(h);
  147.     return err;
  148. }
  149.  
  150.  
  151.  
  152. /*----------------------------------------------------------------------------
  153.     DragText
  154.     
  155.     Drag selected text.
  156.     
  157.     Entry:    ev = pointer to mouse down event record.
  158.             where = location of mouse down event in local coords.
  159.             theTE = handle to TextEdit record.
  160.     
  161.     Exit:    function result = error code.
  162.             *dragged = 
  163.                 true if text was dragged.
  164.                 false if mouse down was not over text selection, or
  165.                     user did not move the mouse before releasing the
  166.                     mouse button.
  167.             *trashed = true if text was dragged to trash.
  168. ----------------------------------------------------------------------------*/
  169.  
  170. OSErr DragText (EventRecord *ev, Point where, TEHandle theTE, 
  171.     Boolean *dragged, Boolean *trashed)
  172. {
  173.     DragReference dragRef;
  174.     OSErr err = noErr;
  175.     Boolean haveDragRef = false;
  176.     RgnHandle dragRgn = nil;
  177.     Handle hText;
  178.     short selStart, selEnd;
  179.     char state;
  180.  
  181.     *dragged = false;
  182.     *trashed = false;
  183.     if (!PtInTEHiliteRgn(where, theTE)) return noErr;
  184.     if (!WaitMouseMoved(ev->where)) return noErr;
  185.     *dragged = true;
  186.     
  187.     hText = (**theTE).hText;
  188.     selStart = (**theTE).selStart;
  189.     selEnd = (**theTE).selEnd;
  190.     
  191.     err = NewDrag(&dragRef);
  192.     if (err != noErr) goto exit;
  193.     haveDragRef = true;
  194.     state = MyHGetState(hText);
  195.     MyHLock(hText);
  196.     err = AddDragItemFlavor(dragRef, 1, 'TEXT', *hText + selStart, selEnd - selStart, 0);
  197.     MyHSetState(hText, state);
  198.     if (err != noErr) goto exit;
  199.     dragRgn = NewRgn();
  200.     err = TEGetHiliteRgn(dragRgn, theTE);
  201.     if (err != noErr) goto exit;
  202.     LocalToGlobalRgn(dragRgn);
  203.     OutlineRegion(dragRgn);
  204.     err = TrackDrag(dragRef, ev, dragRgn);
  205.     if (err != noErr && err != userCanceledErr) goto exit;
  206.     *trashed = DragTargetWasTrash(dragRef);
  207.     DisposeRgn(dragRgn);
  208.     DisposeDrag(dragRef);
  209.     return noErr;
  210.     
  211. exit:
  212.  
  213.     if (haveDragRef) DisposeDrag(dragRef);
  214.     if (dragRgn != nil) DisposeRgn(dragRgn);
  215.     return err;
  216. }